#!/usr/bin/perl


use strict;
use warnings;


my $str;
my @string;
my %Settings;

if ($^O eq "MSWin32" ) {

    # MS Windows environments
    require Win32;
    require Win32::Console;
    require Win32::Console::ANSI;


}
else {

    # UNIX like environments
    use Term::ReadKey;
    use Term::ANSIColor;

}
sub updateTerminalSettings {
    if ($^O eq "MSWin32" ) {

        # MS Windows environments
        my $CONSOLE = new Win32::Console;
        (
            $Settings{TERMINALCOLS},      $Settings{TERMINALROWS},
            $Settings{TERMINALCURSORCOL}, $Settings{TERMINALCURSORROW}
        ) = $CONSOLE->Info();
    }
    else {

        # UNIX like environments
        (
            $Settings{TERMINALCOLS},      $Settings{TERMINALROWS},
            $Settings{TERMINALCURSORCOL}, $Settings{TERMINALCURSORROW}
        ) = GetTerminalSize();
    }
}

&updateTerminalSettings;

print $^O;
my $formatline = "@". "|" x ($Settings{TERMINALCOLS}-2);
my $format = "format STDOUT =
$formatline
\$str
.";

eval $format;
print "\n\n\n\n\n\n";
$string[0] = "   _/                                                _/              ";
$string[1] = "_/_/_/_/    _/_/_/  _/_/_/  _/_/      _/_/_/    _/_/_/  _/_/_/  _/_/ ";
$string[2] = " _/      _/_/      _/    _/    _/  _/    _/  _/    _/  _/    _/    _/";
$string[3] = "_/          _/_/  _/    _/    _/  _/    _/  _/    _/  _/    _/    _/ ";
$string[4] = " _/_/  _/_/_/    _/    _/    _/    _/_/_/    _/_/_/  _/    _/    _/  ";
$string[5] = "                                                                     ";
$string[6] = "Configuration Wizard";

foreach (@string){
    $str = $_;
    write;
}
print "\n\n\n\n\n";

### Questions
my %question = (
    DSM_DIR => "Please setup your DSM_DIR directory:",
    DSM_LOG => "Please setup your DSM_LOG directory:",
    DSM_CONFIG => "Please setup your DSM_CONFIG file:",
    DEBUG => "Would you like to use DEBUG mode?",
    DEFAULTCOLOR => "Would you like to set DEFAULT color?",
    DEFAULTCOLOR => "Would you like to set HEADER color?",
    
    MORE_TSM_SERVER => "Would you like to add other server options to the config file?",
#    DEFAULTCOLOR => "Would you like to use different color?",
#    HEADERCOLOR => "Would you like to use different header color?",
    
);


my %description ;

$description{DSM_DIR} = "

1. DSM Directory

Please type your tsm installation directory, where we find dsmadmc program
If you would like to accept the default option, just press ENTER.
Every other directory, is also accepted..

";

$description{DSM_LOG} = "

2. DSM LOG Directory

Please type your tsm log directory, where you want to ....
If you would like to accept the default option, just press ENTER.
Every other directory, is also accepted..

";


### Init Answer hashs
my %answer;
foreach (keys %question) {
    $answer{$_} = 'NA';
}

### setDefault: minden érték egy szubrutin, ami a hozzá tartozó elem default értékét állítja be
my %setDefault;

### checkDefault: ellenőrzi, hogy a bevitt érték ténylég helyes-e
my %checkDefault;

### Tartalmazza az alapértelemezett értékeket
my %default;



$setDefault{'DSM_DIR'} = sub () {
    if ( defined $ENV{DSM_DIR} ) {
        $default{DSM_DIR} = $ENV{DSM_DIR};
    }
    elsif ( $^O eq "darwin"
        && -d "/Library/Application Support/tivoli/tsm/client/ba/bin/" )
    {
        $default{DSM_DIR} = "/Library/Application Support/tivoli/tsm/client/ba/bin/";
    }  
    elsif ($^O ne "MSWin32" && -d 'C:\Program Files\Tivoli\TSM\baclient' )
    {
        $default{DSM_DIR} = 'C:\Program Files\Tivoli\TSM\baclient';
    }
    else {
      $default{DSM_DIR} = "";
    }
};

$setDefault{'DSM_LOG'} = sub () {
    if ( defined $ENV{DSM_LOG} ) {
        $default{DSM_LOG} = $ENV{DSM_LOG};
    }
    elsif ( $^O eq "darwin"
        && -d "/Library/Application Support/tivoli/tsm/client/ba/bin/" )
    {
        $default{DSM_LOG} = "/Library/Application Support/tivoli/tsm/client/ba/bin/";
    }
    elsif ($^O ne "MSWin32" && -d 'C:\Program Files\Tivoli\TSM\baclient' )
    {
        $default{DSM_LOG} = 'C:\Program Files\Tivoli\TSM\baclient';
    }
    else {
      $default{DSM_LOG} = "";
    }
};

$setDefault{'DSM_CONFIG'} = sub {
    if ( defined $ENV{DSM_CONFIG} ) {
        $default{DSM_CONFIG} = $ENV{DSM_CONFIG};
    }
    elsif ( $^O eq "darwin"
        && -r "/Library/Preferences/Tivoli Storage Manager/dsm.opt"
        && -f "/Library/Preferences/Tivoli Storage Manager/dsm.opt" )
    {
        $default{DSM_CONFIG} = "/Library/Preferences/Tivoli Storage Manager/dsm.opt";
    }
    elsif ($^O ne "MSWin32"
          && -r 'C:\Program Files\Tivoli\TSM\baclient\dsm.opt'
          && -f 'C:\Program Files\Tivoli\TSM\baclient\dsm.opt' )
    {
        $default{DSM_CONFIG} = 'C:\Program Files\Tivoli\TSM\baclient\dsm.opt';
    }
    else {
      $default{DSM_CONFIG} = "";
    }
};

$setDefault{'DEBUG'} = sub {
    $default{DEBUG} = "false";

};

$setDefault{'MORE_TSM_SERVER'} = sub {
    $default{'MORE_TSM_SERVER'} = "no";
};



$checkDefault{'DSM_CONFIG'} = sub {
    return 0 if (-r "$answer{'DSM_CONFIG'}" && -f"$answer{'DSM_CONFIG'}" );
    print "I think this is not a file or not readable!\n" if ( $answer{'DSM_CONFIG'} ne 'NA');
    return 1;
};
$checkDefault{'DSM_DIR'} = sub {
    return 0 if ( -d "$answer{'DSM_DIR'}");
    print "I think this is not a directory!\n" if ( $answer{'DSM_DIR'} ne 'NA');
    return 1;
};
$checkDefault{'DSM_LOG'} = sub {
    return 0 if (-d "$answer{'DSM_LOG'}");
    print "I think this is not a directory!\n" if ( $answer{'DSM_LOG'} ne 'NA' );
    return 1;
    };
$checkDefault{'DEBUG'} = sub {
    if ($answer{DEBUG} !~ m/true|false/i) {
        print "Possible answers are: true or false \n" if ( $answer{DEBUG} ne 'NA');
        return 1;
    };
    $answer{DEBUG} = '0' if ($answer{DEBUG} eq "false");
    $answer{DEBUG} = '1' if ($answer{DEBUG} eq "true");
    return 0;
};
$checkDefault{'MORE_TSM_SERVER'} = sub {
    if ($answer{MORE_TSM_SERVER} !~ m/yes|no/i) {
        print "Possible answers are: yes or no \n" if ( $answer{MORE_TSM_SERVER} ne 'NA');
        return 1;
    };
    $answer{MORE_TSM_SERVER} = '0' if ($answer{MORE_TSM_SERVER} =~ m/no/i);
    $answer{MORE_TSM_SERVER} = '1' if ($answer{MORE_TSM_SERVER} =~ m/yes/i);
    return 0;
};
sub getAnswer($) {
    my $hashKey = $_[0];
    my $answer = <STDIN>;
    chomp($answer);
    if ( $answer =~ m/^BACK$/i ) {
        return "BACK";
    }
    elsif ( $answer =~ m/^NEXT$/i ) {
        return "NEXT";
    }
    elsif ( $answer =~ m/^QUIT$/i ) {
        exit 99;
    }
    else {
        if ($answer eq "") {
            $answer = $default{$hashKey};
        }
        return "$answer";
    }
}

sub askQuestion() {
    my $hashKey = $_[0]; 
    &{$setDefault{$hashKey}};
    print $description{$hashKey} if (defined $description{$hashKey});
    while (&{$checkDefault{$hashKey}}) {
        print "$question{$hashKey} [$default{$hashKey}] : ";
        $answer{$hashKey} = &getAnswer($hashKey);
    }
}

sub getTSMServerParameters(){
    print "TSM Server Name: "; my $name = <STDIN>;
    print "Server Address (IP or hostname): "; my $ipaddress = <STDIN>;
    if ($^O ne "MSWin32") {
        print "TCP port:"; my $tcpport = <STDIN>;
    }
    print "User name: "; my $username = <STDIN>;
    print "Password: "; my $password = <STDIN>;
    $answer{MORE_TSM_SERVER} = 'NA';
    return &askQuestion('MORE_TSM_SERVER');    
}



&askQuestion("DSM_DIR");
&askQuestion("DSM_LOG");
&askQuestion("DSM_CONFIG");
&askQuestion("DEBUG");

### Server sections:

while ( $answer{MORE_TSM_SERVER} eq "1" || $answer{MORE_TSM_SERVER} eq "NA") {
    print "Add a TSM server parameters\n";
    &getTSMServerParameters;
}


#Summary


